package com.android.launcher3;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

/**
 * GridView adapter to show the list of applications and shortcuts
 */
public class ApplicationsAdapter extends ArrayAdapter<ApplicationInfo> {

	public static final String TAG = "ApplicationsAdapter";
	ArrayList<ApplicationInfo> apps;
	private final LayoutInflater mInflater;

	/**
	 * Creates a new ApplicationsAdapter object.
	 * 
	 * @param context
	 * @param apps
	 */
	public ApplicationsAdapter(final Context context,
			final ArrayList<ApplicationInfo> apps) {
		super(context, 0, apps);
		this.apps = apps;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {
		ApplicationInfo localApplicationInfo1 = (ApplicationInfo) getItem(position);

		if (convertView == null) {
			LayoutInflater localLayoutInflater = this.mInflater;
			convertView = localLayoutInflater.inflate(
					R.layout.application_boxed, parent, false);
		}

		MenuItemView localMenuItemView1 = (MenuItemView) convertView;
		localMenuItemView1.setImageDrawable(new FastBitmapDrawable(
				localApplicationInfo1.iconBitmap));
		localMenuItemView1.setText(localApplicationInfo1.title);

		// String packageName = null;
		// String className = null;
		//
		// if ((localApplicationInfo1.intent != null)
		// && (localApplicationInfo1.intent.getComponent() != null)) {
		// packageName = localApplicationInfo1.intent.getComponent()
		// .getPackageName();
		// className = localApplicationInfo1.intent.getComponent()
		// .getClassName();
		// }
		//
		// Drawable localDrawable = Utilities.getDrawableIconBg(packageName,
		// className,
		// this.getContext(),
		// localApplicationInfo1.systemApp);
		//
		// localMenuItemView1.setBackgroundDrawable(localDrawable);
		ImageView localImageView = (ImageView) localMenuItemView1
				.findViewById(R.id.icon);
		ImageView.ScaleType localScaleType1 = localImageView.getScaleType();

		float f1;
		float f2;
		float f3;
		float f5;
		Matrix localMatrix1;

		if ((localScaleType1 == ImageView.ScaleType.MATRIX)
				&& (localApplicationInfo1.iconBitmap != null)) {
			f1 = localApplicationInfo1.iconBitmap.getWidth();
			f2 = localApplicationInfo1.iconBitmap.getHeight();
			f3 = getContext().getResources().getDimensionPixelSize(
					R.dimen.menu_item_icon_width);
			f5 = getContext().getResources().getDimensionPixelSize(
					R.dimen.menu_app_icon_size);
			localMatrix1 = localImageView.getImageMatrix();

			if (localMatrix1 != null) {
				float f14 = f5 / f1;
				float f15 = f5 / f2;
				localMatrix1.preScale(f14, f15);

				float f18 = (f3 - f5) / 2.0F;
				float f19 = (f3 - f5) / 2.0F;
				localMatrix1.postTranslate(f18, f19);
			} else {
				Matrix localMatrix2 = new Matrix();
				float f6 = f5 / f1;
				float f7 = f5 / f2;
				localMatrix2.preScale(f6, f7);

				float f10 = (f3 - f5) / 2.0F;
				float f11 = (f3 - f5) / 2.0F;
				localMatrix2.postTranslate(f10, f11);
				localImageView.setImageMatrix(localMatrix2);
			}
		}

		convertView.setTag(localApplicationInfo1);

		return convertView;
	}
}
